$().ready(function(){
    let page = {
        p:1,
        num:20,
        status:10,
        start_time:'',
        end_time:''
    };
    function init(){
        //获取当前日期
        page.today = getNow(1);
        getOrderList(page.p,page.num,page.status,page.start_time,page.end_time);
        //开始时间
        datePicker('start_time','YYYY-MM-DD',page.today,function(dates){
            page.start_time = dates;
        });
        //结束时间
        datePicker('end_time','YYYY-MM-DD',page.today,function(dates){
            page.end_time = dates;
        })
    }
    init();
    //确认发货
    $('#order_list').on('click','.fh',function(){
        let id = $(this).data('id');
        console.log(id);
        getAjax(`${global.apiUrl}/kitchen_shop_fahuo`,res=>{
            console.log(res);
            if(res == 1){
                alert('确认发货！');
                getOrderList(page.p,page.num,page.status,page.start_time,page.end_time);
            }
        },{
            order_id:id
        })
    });
    //查看订单信息
    $('#orderModal').on('show.bs.modal',function(event){
        let modal = $(this);
        let btnThis = $(event.relatedTarget);
        let id = btnThis.data('id');
        console.log(id);
        let list = page.order_list;
        let order_content = {};
        for(let i in list){
            if(list[i].id == id){
                order_content = list[i];
            }
        }
        $('#order_num').val(order_content.order_num);
        $('#order_name').val(order_content.name);
        $('#order_phone').val(order_content.phone);
        $('#order_address').val(order_content.sheng+order_content.shi+order_content.qu+order_content.address);
        let html = '<label>购买产品：</label>';
        let goods = order_content.content;
        for(let j in goods){
            html += `
                <input type="text" class="form-control"  placeholder="设置商品单位" value="商品名称：${goods[j].product_name} / 数量：${goods[j].num} / 单价：${goods[j].one_price} / 总价：${goods[j].all_price}" disabled>

            `
        }
        $('#order_goods_list').html(html);
        $('#order_price').val(order_content.order_price);
    })
    //订单状态切换
    $('#order_status').on('change',function(){
        let status = $(this).children('option:selected').val();
        page.status = status;
        page.p = 1;
        getOrderList(page.p,page.num,page.status,page.start_time,page.end_time);
    });
    //订单时间筛选
    $('#time_sel').on('click',function(){
        let start = $('#start_time').val();
        let end = $('#end_time').val();
        page.p = 1;
        page.start_time = start;
        page.end_time = end;
        getOrderList(page.p,page.num,page.status,page.start_time,page.end_time);
    });
    //计算状态
    function calcStatus(status){
        let text = status==0?'待支付':(status==1?'待发货':(status==2?'待收货':(status==3?'已完成':'')));
        return text;
    }
    //获取订单
    function getOrderList(p,num,status,start,end){
        let start_time = '';
        let end_time = '';
        if(start&&end){
            start_time = dateTotimestamp(start+' 00:00:00');
            end_time = dateTotimestamp(end+' 23:59:59');
        }
        getAjax(`${global.apiUrl}/admin_get_community_service_kitchen_product_order`,res=>{
            console.log(res);
            let list = res.list;
            let html = '';
            if(list.length == 0){
                $('#page').paging({
                    pageNo:0,
                    totalPage:0,
                    totalSize:0,
                    callback:function(page){
                        getOrderList(page,num,status,start,end);
                    }
                })
                html = `
                    <tr>
                        <td colspan="7" style="font-size: 14px;text-align: center;color:#aaa;">无订单</td>
                    </tr>
                `
            }else{
                page.order_list = list;
                $('#page').paging({
                    pageNo:p,
                    totalPage:Math.ceil(res.count/num),
                    totalSize:res.count,
                    callback:function(pages){
                        page.p = pages;
                        getOrderList(pages,num,status,start,end);
                    }
                });
                for(let i in list){
                    html += `
                     <tr>
                        <td>${list[i].order_num}</td>
                        <td>${setTime2(list[i].createtime)}</td>
                        <td>${list[i].order_price}</td>
                        <td>${list[i].name}</td>
                        <td>${list[i].phone}</td>
                        <td>${calcStatus(list[i].status)}</td>
                        <td>
                            <button class="btn btn-primary ck" data-id="${list[i].id}" data-toggle="modal" data-target="#orderModal">查看</button>
                            <input type="${list[i].status==1?'button':'hidden'}" class="btn btn-success fh" value="发货" data-id="${list[i].id}">
                        </td>
                    </tr>
                
                `
                }

            }
            $('#order_list').html(html);
        },{
            p:p,
            num:num,
            kitchen_id:global.kitchen_id,
            status:status,
            start_time:start_time,
            end_time:end_time
        })
    }
})
